package xkp.lesson.utils;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import xkp.lesson.dao.JobInfoDao;
import xkp.lesson.entity.JobInfoEntity;

import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

public class ReadJobData {

    //https://www.zhipin.com/c101180100/?query=java&page=2&ka=page-2
    public static void readData(String cityNum, String kw, int page) throws IOException, SQLException {
        System.out.println(page);
        String url = "https://www.zhipin.com/" + cityNum + "?query=" + kw + "&page=" + page + "&ka=page-" + page;
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        //屏蔽日志信息
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log",
                "org.apache.commons.logging.impl.NoOpLog");
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        //支持JavaScript
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setActiveXNative(false);
        // webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(5000);

        System.out.println(url);
        HtmlPage rootPage = webClient.getPage(url);
        //设置一个运行JavaScript的时间
        webClient.waitForBackgroundJavaScript(5000);
        String html = rootPage.asXml();

        Document document = Jsoup.parse(html);
        Elements elementList = document.select(".job-list>ul>li>.job-primary");
        List<JobInfoEntity> list = new ArrayList<>();
        for (Element element : elementList) {
            JobInfoEntity jobInfoEntity = new JobInfoEntity();
            jobInfoEntity.setJobId(element.selectFirst(".primary-box").attr("data-jobid"));
            jobInfoEntity.setJobName(element.selectFirst(".job-name").text());
            jobInfoEntity.setJobArea(element.selectFirst(".job-area").text());

            //job_salary
            jobInfoEntity.setJobSalary(element.selectFirst(".job-limit>span").text());
            String[] sss = element.selectFirst(".job-limit>p").html().split("<em class=\"vline\"></em>");
            //job_exprience
            jobInfoEntity.setJobExprience(sss[0]);
            jobInfoEntity.setJobEducation(sss[1]);
            jobInfoEntity.setCompanyName(element.selectFirst(".company-text>.name").text());
            jobInfoEntity.setSearchCity(cityNum);
            jobInfoEntity.setSearchKw(kw);
            list.add(jobInfoEntity);
        }
        JobInfoDao jobInfoDao = new JobInfoDao();
        jobInfoDao.batchInsert(list);
        if (document.getElementsByClass("page").size() > 0) {
            Element pagediv = document.getElementsByClass("page").get(0);
            Element nexta = pagediv.getElementsByClass("next").get(0);
            if (nexta.attr("href").contains("javascript")) {
                JOptionPane.showMessageDialog(null, "数据爬取完毕");
            } else {
                // 还可以继续爬取数据
                readData(cityNum, kw, page + 1);
            }
        }
    }
}

